unit u_mesh;

interface

uses
  u_load,
  u_form,
  sysutils,
  System.Drawing,
  Microsoft.DirectX.Direct3D,
  //Microsoft.DirectX.Direct3DX,
  Microsoft.DirectX;

type



t_statut = (dynamique, statique, conteneur);

t_stat_coor_obj_map = record
  statut: t_statut;
  objet_i: integer;
  position_j: integer;
end;

tdepart = record
  etage: integer;
  x: integer;
  y: integer;
end;




tmesh = record
  nom: string;
  mesh: mesh;
  materials: material;
  texture: Texture;
end;

Fmesh = record
  x, y : single;
  mesh : Mesh;
  Texture : Texture;
end;

t_tab_mesh = array of tmesh;

c_mesh = class
  private
    nbrobj: integer;
    dep_etage: integer;
    dep_x, dep_y: integer;
  public
    taillex: integer;
    tailley: integer;
    map1 : T_Map;
    nbretage: integer;
    map2 : t_map_obj;
    map_ok: boolean;
    tab_mesh: t_tab_mesh;

  plop : FMesh;
    procedure suppr_obj_res(x, y, z: integer);
    procedure loader_mesh( device: device; etage: integer);
    procedure loadmap(path: string);
    procedure suppr_obj(x, y, z: integer; var tab_cont: t_tab_cont);
    constructor create(path: string);
    function dep_eta: tdepart;
    function don_stat_objet(x, y, z: integer): t_stat_coor_obj_map;
    procedure supp_map1_2(x, y, z: integer; stat: t_stat_coor_obj_map; var tab_cont: t_tab_cont);
    procedure ajout_obj(x, y, z: integer; contenu: t_contenu);



end;



implementation

uses
u_engine;

constructor c_mesh.create;
begin
  inherited create;
  loadmap(path);
end;


procedure c_mesh.loadmap;
var
 cacaprout: t_map;
begin
   map_ok := load_map(map1 , taillex, tailley, nbretage, nbrobj, dep_etage, dep_x, dep_y, path);
   if map_ok then
     begin
     cacaprout := map1;
     map2 := tri_map(cacaprout, taillex, tailley, nbretage, dep_etage, nbrobj)
     end;
end;

procedure c_mesh.ajout_obj(x: Integer; y: Integer; z: Integer; contenu: t_contenu);
var
  obj: objet_map;
  i: Integer;
begin
  obj._type := contenu._type;
  obj.nom := contenu.nom;
  if obj._type = 'Argent' then
    obj.quantite := contenu.carac1
  else
    if obj._type = 'Processeur' then
      obj.vitesse := contenu.carac1
    else
      if obj._type = 'Carte graphique' then
        obj.puissance := contenu.carac1
      else
        if obj._type = 'Ram' then
          obj.taille := contenu.carac1
        else
          if obj._type = 'Disque dur' then
            obj.capacite := contenu.carac1
          else
            if obj._type = 'Armes' then
              begin
                obj.portee := contenu.carac1;
                obj.degat := contenu.carac2;
              end;
  map1[z, x, y] := obj;
  for i := 0 to length(map2[z]) - 1 do
    begin
      if (map2[z][i].objet._type = obj._type) and (map2[z][i].objet.nom = obj.nom) then
        begin
          SetLength(map2[z][i].tab_etage, length(map2[z][i].tab_etage) + 1);
          map2[z][i].tab_etage[high(map2[z][i].tab_etage)].x := x;
          map2[z][i].tab_etage[high(map2[z][i].tab_etage)].y := y;
        end;
    end;
    
end;

procedure c_mesh.loader_mesh( device: device; etage: integer);
var
  nbmesh: integer;
  mat: array of extendedmaterial;
begin
  nbmesh := 0;
      while nbmesh < length(map2[etage]) do
        begin
          nbmesh := nbmesh + 1;
          SetLength(tab_mesh, nbmesh);
          if map2[etage][nbmesh - 1].objet.nom = 'Depart' then
            begin
//              tiny.x := map2[etage][nbmesh - 1].objet.x;
//              tiny.y := map2[etage][nbmesh - 1].objet.y;

              plop.mesh:=Mesh.FromFile('meshes\lolm.x', MeshFlags.SystemMemory, device,mat);
              plop.Texture := TextureLoader.FromFile(device, 'meshes\image005.jpg');

              self.tab_mesh[nbmesh - 1].nom := map2[etage][nbmesh - 1].objet.nom;
              self.tab_mesh[nbmesh - 1].mesh := mesh.FromFile('meshes\sol.x',
                                                              meshflags.Systemmemory,
                                                              device,
                                                              mat);
              Self.tab_mesh[nbmesh - 1].materials := mat[0].Material3D;
              Self.tab_mesh[nbmesh - 1].texture := TextureLoader.FromFile(device,
                                                                          'meshes\mur1.jpg');
              map1[etage, map2[etage][nbmesh - 1].tab_etage[0].x, map2[etage][nbmesh - 1].tab_etage[0].y]._type := 'none'
            end
          else
          if map2[etage][nbmesh - 1].objet.nom = 'Monte' then
            begin
              self.tab_mesh[nbmesh - 1].nom := map2[etage][nbmesh - 1].objet.nom;
              self.tab_mesh[nbmesh - 1].mesh := mesh.FromFile(map2[etage][nbmesh - 1].objet.mesh,
                                                              meshflags.Systemmemory,
                                                              device,
                                                              mat);
              Self.tab_mesh[nbmesh - 1].texture := TextureLoader.FromFile(device,
                                                                          'meshes\PLATEOX2.jpg');
            end
          else

          if map2[etage][nbmesh - 1].objet.nom = 'none' then
            begin
              self.tab_mesh[nbmesh - 1].nom := map2[etage][nbmesh - 1].objet.nom;
              self.tab_mesh[nbmesh - 1].mesh := mesh.FromFile('meshes\sol.x',
                                                              meshflags.Systemmemory,
                                                              device,
                                                              mat);
              Self.tab_mesh[nbmesh - 1].materials := mat[0].Material3D;
              Self.tab_mesh[nbmesh - 1].texture := TextureLoader.FromFile(device,
                                                                          'meshes\mur1.jpg');
            end
          else
            begin
              self.tab_mesh[nbmesh - 1].nom := map2[etage][nbmesh - 1].objet.nom;
              self.tab_mesh[nbmesh - 1].mesh := mesh.FromFile(map2[etage][nbmesh - 1].objet.mesh,
                                                              meshflags.Systemmemory,
                                                              device,
                                                              mat);
              Self.tab_mesh[nbmesh - 1].materials := mat[0].Material3D;
              Self.tab_mesh[nbmesh - 1].texture := TextureLoader.FromFile(device,
                                                                        'meshes\' + mat[0].TextureFilename);
            end;
        end;
end;

function c_mesh.dep_eta;
begin
  result.etage := dep_etage;
  Result.x := dep_y ;
  Result.y := dep_x;
end;

procedure c_mesh.supp_map1_2(x: Integer; y: Integer; z: Integer; stat: t_stat_coor_obj_map; var tab_cont: t_tab_cont);
var
  tempx, tempy: integer;
  i, j: integer;
  temp_type: string;
  temp_cont: t_contenu;
begin
  if stat.statut = dynamique then
    begin
      temp_cont.nom := Self.map2[z][stat.objet_i].objet.nom;
      temp_cont._type := Self.map2[z][stat.objet_i].objet._type;
      temp_cont.equip := false;
      while (i <= high(tab_cont)) and (temp_cont._type <> tab_cont[i]._type) and (temp_cont.nom <> tab_cont[i].nom) do
        i := i + 1;
      if i > high(tab_cont) then
        begin
          if temp_cont._type = 'Ram' then
            begin
            temp_cont.carac1 := Self.map2[z][stat.objet_i].objet.taille;
            temp_cont.quantite := 1;
            end;
          if temp_cont._type = 'Carte graphique' then
            begin
              temp_cont.carac1 := Self.map2[z][stat.objet_i].objet.puissance;
              temp_cont.quantite := 1;
            end;
          if temp_cont._type = 'Disque dur' then
            begin
              temp_cont.carac1 := Self.map2[z][stat.objet_i].objet.capacite;
              temp_cont.quantite := 1;
            end;
          if temp_cont._type = 'Armes' then
            begin
              temp_cont.carac1 := Self.map2[z][stat.objet_i].objet.portee;
              temp_cont.carac2 := Self.map2[z][stat.objet_i].objet.degat;
              temp_cont.quantite := 1;
            end;
          SetLength(tab_cont, length(tab_cont) + 1);
          myplayer.mychat.add_text('Vous venez de ramasser: '+ temp_cont.nom);
          tab_cont[high(tab_cont)] := temp_cont;
        end
      else
        begin
          if temp_cont._type = 'Argent' then
            begin
              temp_cont.quantite := Self.map2[z][stat.objet_i].objet.quantite;
              tab_cont[i].quantite := tab_cont[i].quantite + temp_cont.quantite;
              myplayer.mychat.add_text('Vous venez de ramasser: '+ inttostr(temp_cont.quantite) + ' pieces d''or');
            end
          else
            begin
          temp_cont := tab_cont[i];
          temp_cont.quantite := temp_cont.quantite + 1;
          myplayer.mychat.add_text('Vous venez de ramasser un autre: '+ temp_cont.nom)
            end;
        end;
      self.map2[z][stat.objet_i].tab_etage[stat.position_j] := self.map2[z][stat.objet_i].tab_etage[high(self.map2[z][stat.objet_i].tab_etage)];
      SetLength(self.map2[z][stat.objet_i].tab_etage, length(self.map2[z][stat.objet_i].tab_etage) - 1);
      if (self.map1[z, x + 1, y]._type = 'none') or
         (self.map1[z, x + 1, y]._type = 'Sol') then
           begin
             tempx := x + 1;
             tempy := y;
             temp_type := self.map1[z, x + 1, y]._type;
           end
      else
        if (self.map1[z, x - 1, y]._type = 'none') or
           (self.map1[z, x - 1, y]._type = 'Sol') then
             begin
               tempx := x - 1;
               tempy := y - 1;
               temp_type := self.map1[z, x - 1, y]._type;
             end
        else
          if (self.map1[z, x, y + 1]._type = 'none') or
             (self.map1[z, x, y + 1]._type = 'Sol') then
               begin
                 tempx := x;
                 tempy := y + 1;
                 temp_type := self.map1[z, x , y + 1]._type;
               end
          else
            if (self.map1[z, x, y - 1]._type = 'none') or
               (self.map1[z, x, y - 1]._type = 'Sol') then
                 begin
                   tempx := x;
                   tempy := y - 1;
                   temp_type := self.map1[z, x, y - 1]._type;
                 end;
      self.map1[z, x, y].mesh := self.map1[z, tempx, tempy].mesh;
      self.map1[z, x, y]._type := self.map1[z, tempx, tempy]._type;
      self.map1[z, x, y].nom := self.map1[z, tempx, tempy].nom;
      j := 0;
      while (j <= high(self.map2[z])) and (self.map2[z][j].objet._type <> temp_type) do
                   begin
                     j := j + 1
                   end;
                 SetLength(self.map2[z][j].tab_etage, length(self.map2[z][j].tab_etage) + 1);
                 self.map2[z][j].tab_etage[high(self.map2[z][j].tab_etage)].x := x;
                 self.map2[z][j].tab_etage[high(self.map2[z][j].tab_etage)].y := y;
    end
//  else
//    if stat.statut = conteneur then
//      begin
//        if Self.map2[z][stat.objet_i].objet.contenu.nom = '' then
//          exit;
//        temp_cont := Self.map2[z][stat.objet_i].objet.contenu;
//        SetLength(tab_cont, length(tab_cont) + 1);
//        tab_cont[high(tab_cont)] := temp_cont;
//        Self.map2[z][stat.objet_i].objet.contenu.nom := '';
//        Self.map2[z][stat.objet_i].objet.contenu._type := '';
//        Self.map2[z][stat.objet_i].objet.contenu.carac1 := 0;
//        Self.map2[z][stat.objet_i].objet.contenu.carac2 := 0;
//      end;

end;

procedure c_mesh.suppr_obj_res(x, y, z: integer);
var
  i: Integer;
  j: Integer;
begin
  self.map1[z, x, y]._type := 'none';
  self.map1[z, x, y].nom := 'none';
  for i := 0 to length(map2[z]) - 1 do
    begin
      for j := 0 to length(map2[z][i].tab_etage) - 1 do
        begin
          if (map2[z][i].tab_etage[j].x = x) and (map2[z][i].tab_etage[j].y = y) then
            map2[z][i].tab_etage[j].x := map2[z][i].tab_etage[length(map2[z][i].tab_etage) - 1].x;
            map2[z][i].tab_etage[j].y := map2[z][i].tab_etage[length(map2[z][i].tab_etage) - 1].y;
            SetLength(map2[z][i].tab_etage, length(map2[z][i].tab_etage) - 1);
        end;
    end;
end;


procedure c_mesh.suppr_obj;
var
  i, k, j: integer;
  temp: t_stat_coor_obj_map;
begin
  temp := Self.don_stat_objet(x, y, z);
  self.supp_map1_2(x, y, z, temp, tab_cont);
end;


function c_mesh.don_stat_objet(x: Integer; y: Integer; z: Integer): t_stat_coor_obj_map;
var
  i, k, j: integer;
  temp_type: string;
begin
  temp_type := Self.map1[z, x, y]._type;
  i := low(self.map2[z]);
  while (i <= high(self.map2[z])) and (j <= high(self.map2[z])) do
    begin
      k := low(self.map2[z][i].tab_etage);
      while k <= high(self.map2[z][i].tab_etage)  do
        begin
          if (self.map2[z][i].tab_etage[k].x = x) and
             (self.map2[z][i].tab_etage[k].y = y) and
             (self.map2[z][i].objet._type = temp_type) then
               begin
                 if (self.map2[z][i].objet._type = 'Argent') or
                    (self.map2[z][i].objet._type = 'Ram') or
                    (self.map2[z][i].objet._type = 'Carte graphique') or
                    (self.map2[z][i].objet._type = 'Disque dur') or
                    (self.map2[z][i].objet._type = 'Armes') then
                      result.statut := dynamique
                 else
                   if (self.map2[z][i].objet._type = 'Coffre') then
                     result.statut := conteneur
                   else
                     result.statut := statique;
                 result.objet_i := i;
                 Result.position_j := k;
                 j := high(self.map2[z]) + 1;
                 k := high(self.map2[z][i].tab_etage);
               end;
          k := k + 1
        end;
      i := i + 1;
    end;
end;


end.
